/**
 * 发送请求，获取用户收货地址  获取订单中心的数据
 */

import { reqGetUserAddressList, reqTradeInfoData } from '@/api';

const state = {
  userAddressList: [],
  tradeInfo: {},
};
const actions = {
  // 1. 获取用户的收获地址
  async getUserAddressList() {
    const result = await reqGetUserAddressList();
    
    if (result.code === 200) {
      commit('SAVE_GET_USER_ADDRESS_LIST', result.data);
    } else {
      console.log(result.message);
    }
  },
  // 2. 获取订单中的商品数据
  async getTradeInfoData({ commit }) {
    
    const result = await reqTradeInfoData();
    console.log('123')
    console.log(result);
    if (result.code === 200) {
      commit('SAVE_TRADE_INFO_DATA', result.data);
    } else {
      console.log(result.message);
    }
  },
};
const mutations = {
  // 1. 存储用户地址列表
  SAVE_GET_USER_ADDRESS_LIST(state, addressList) {
    state.userAddressList = addressList;
  },
  // 2. 存储商品数据
  SAVE_TRADE_INFO_DATA(state, tradeInfo) {
    state.tradeInfo = tradeInfo;
  },
};
const getters = {
  // 商品列表
  detailArrayList(state) {
    return state.tradeInfo.detailArrayList;
  },
  // 商品总金额
  originalTotalAmount(state) {
    return state.tradeInfo.originalTotalAmount;
  },
  // 实付金额
  totalAmount(state) {
    return state.tradeInfo.totalAmount;
  },
  // 商品数量
  totalNum(state) {
    return state.tradeInfo.totalNum;
  },
  // 订单编号
  tradeNo(state) {
    return state.tradeInfo.tradeNo;
  },
};

export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters,
};
